<?php
session_start();
include "TopSdk.php";
$appkey = '23249576';
$secret = '1ba903d781cad1a10eea83fa91b38ea3';
global $mobile,$conn,$member_id,$tablepre,$newMobile;
$tablepre = $config['tablepre'];
$op = isset($_GET['op']) ? $_GET['op'] : '';
$mobile = isset($_GET['mobile']) ? $_GET['mobile'] : '';
$newMobile = isset($_GET['newMobile'])?$_GET['newMobile']:0;
$flag = isset($_GET['flag'])?intval($_GET['flag']):0;
$conn = mysqli_connect($dbserver,$dbuser,$dbpasswd,$dbname);
$member_id=$_SESSION['member_id'];
if($mobile && $op)
{
    if ($op == 'verCode') {
        $code = rand(1,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
        $_SESSION['verCode'] = $code;
        $_SESSION['dialogFlag'] = $flag;
        $_SESSION['verTime'] = time();
//        $t= '{"result":{"code":"1","datas":{"task_id":"922906944557206385"},"message":"SUCCESS","successful":"true"},"request_id":"13oz5ebln326w"}';
//        exit($t);
        $templateId = "'template_id':'850913733'";
        $context = "'context':{'code':$code,'minute':10},'mobile':$mobile";
        $sendRequest = "{".$templateId.",".$context."}";
        $c = new TopClient;
        $c->appkey = $appkey;
        $c->secretKey = $secret;
        $req = new OpenSmsSendmsgRequest;

        $req->setSendMessageRequest($sendRequest);
        $resp = $c->execute($req);
        exit(json_encode($resp));
    }
    elseif($op=='checkCode')
    {
        $verCode = isset($_GET['verCode'])?$_GET['verCode']:'';
        if(isset($_SESSION['verTime']) && time()-$_SESSION['verTime']<600) { //验证码超时检查
            if ($_SESSION['verCode'] == $verCode && $_SESSION['dialogFlag'] == $flag) {
                echo('{"result":true}');
            } else {
                echo('{"result":false}');
            }
        }
        else
        {
            $result = (object)array('result'=>'timeOut');
            echo ('{"result":"验证码已过期！"}');
        }
    }
    elseif($op=="checkSuccess")
    {
        unset($_SESSION['verCode']);
        $_SESSION['dialogFlag'] = 0;
        switch($flag)
        {
            case 0:  //空标志
                echo ('{"result":"操作失败！"}');
                break;
            case 1:  //绑定手机
                if(bindMemberTel())
                {
                    echo ('{"result":true,"message":"绑定成功！"}');
                }
                else{
                    ('{"result":false,"message":"操作失败！"}');
                }
                break;
            case 2:  //更换手机

                if(changeMemberTel())
                {
                    echo ('{"result":true,"message":"修改成功！"}');
                }else{
                    ('{"result":false,"message":"操作失败！"}');
                }
                break;
            case 3:  //解绑手机
                if(unbindMemberTel())
                {
                    echo ('{"result":true,"message":"解除绑定成功！"}');
                }else{
                    ('{"result":false,"message":"操作失败！"}');
                }
                break;
            default:
                break;
        }
    }
}
else{
    echo('{"result":"op or mobile missed"}');
}

function bindMemberTel()
{
    $sql = "update `".$GLOBALS['tablepre']."member` set member_mobile = '".$GLOBALS['mobile']."', member_mobile_bind=1 where member_id=".$GLOBALS['member_id'];
    if( mysqli_query($GLOBALS['conn'],$sql))
    {
        return true;
    }
    return false;
}

function changeMemberTel()
{
    if($GLOBALS['newMobile']=="" || $GLOBALS['newMobile']==0) return false;
    $sql = "update `".$GLOBALS['tablepre']."member` set member_mobile = '".$GLOBALS['newMobile']."', member_tel = '".$GLOBALS['newMobile']."'  where member_id=".$GLOBALS['member_id'];
    if( mysqli_query($GLOBALS['conn'],$sql))
    {
        return true;
    }
    return false;
}

function unbindMemberTel()
{
    $sql = "update `".$GLOBALS['tablepre']."member` set member_mobile='', member_mobile_bind=0 where member_id=".$GLOBALS['member_id'];
    if( mysqli_query($GLOBALS['conn'],$sql))
    {
        return true;
    }
    return false;
}